package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.UserCenterService;
import com.zerdoor.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Project: acgplat
 * Module ID: 014
 * Comments: 用户管理
 * JDK version used: JDK1.7
 * Namespace: controller
 * Author：xiaobo
 * Create Date：2014-11-24
 * Modified By：   <修改人中文名或拼音缩写>
 * Modified Date:  <修改日期，格式:YYYY-MM-DD>
 * Why & What is modified  <修改原因描述>
 * Version:                  <版本号>
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserCenterService userCenterService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(Model model){
        model.addAttribute("levels",userService.getArtistLevels());
        return "/user/view";
    }

    @RequestMapping(value = "/view",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> view(HttpServletRequest request,int page,int rows){
        Map<String, Object> result = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
        result.put("userName",request.getParameter("userName"));
        List<Map<String, Object>> list = userService.getUsers(result);
        PageInfo pageInfo = new PageInfo(list);
        result.put("rows", list);
        result.put("total", pageInfo.getTotal());
        return result;
    }
    @RequestMapping("detail")
    public ModelAndView detail(int userId){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user", userService.getUserByUserId(userId));
        modelAndView.setViewName("user/detail");
        return modelAndView;
    }

    /**
     * 改变艺术家等级
     * @param userId 用户编号
     * @param level 艺术家等级
     * @return
     */
    @RequestMapping(value="changeArtistLevel", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> changeArtistLevel(int userId,int level,HttpServletRequest request){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("userId",userId);
        result.put("level",level);
        try {
            userService.updateArtisLevel(result);

            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curManager");
            Map<String,Object> msg = new HashMap<String,Object>();

            msg.put("sender",userInfo.getUserId());
            msg.put("receiver",userId);
            msg.put("theme","艺术家等级变化");
            msg.put("content","您的艺术家等级发生变化了。");
            userCenterService.addUserMessage(msg);
        } catch (Exception e) {
            result.put("errormsg","提升艺术家等级失败");
        }
        return result;
    }

    /**
     * 身份认证
     * @param userId 用户编号
     * @return
     */
    @RequestMapping(value="changeNameAuth", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> changeNameAuth(int userId,HttpServletRequest request){
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            userService.updateNameAuth(userId);

            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curManager");
            Map<String,Object> msg = new HashMap<String,Object>();

            msg.put("sender",userInfo.getUserId());
            msg.put("receiver",userId);
            msg.put("theme","身份认证通过");
            msg.put("content","您的身份认证信息通过了。");
            userCenterService.addUserMessage(msg);
        } catch (Exception e) {
            result.put("errormsg","身份认证失败");
        }
        return result;
    }
}
